package com.aaa.zm.filter;

import com.aaa.common.util.LoadPropertyUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author ：杨
 * @date ：Created in 2020/7/25 16:07
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class IlligelCharFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pro";
    }

    @Override
    public int filterOrder() {
        return 10;
    }

    @Override
    public boolean shouldFilter() {
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletResponse response = currentContext.getResponse();
        HttpServletRequest request = currentContext.getRequest();
        Enumeration<String> parameterNames = request.getParameterNames();
        Properties properties = LoadPropertyUtil.LoadPropertyByPath("filter_config.properties");
        String illigel_char = properties.getProperty("illigel_char");
        String[] icharArray = illigel_char.split(",");
        response.setCharacterEncoding("utf-8");
        while (parameterNames.hasMoreElements()){
            String paramName = parameterNames.nextElement();
            String parameter = request.getParameter(paramName);
            for (String ichar : icharArray) {
                if (parameter.contains(ichar)){
                    try {
                        response.sendError(403,"本网站拒绝一切敏感词和不道德词语。。。");
                        return null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}
